package com.petadoption.serverclient.controller;

import com.petadoption.serverclient.entity.User;
import com.petadoption.serverclient.exception.ServiceException;
import com.petadoption.serverclient.service.IUserService;
import com.petadoption.serverclient.utils.RedisUtil;
import com.petadoption.serverclient.utils.Result;
import com.petadoption.serverclient.utils.TokenUtil;
import com.petadoption.serverclient.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;


@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService usersService;
    @Resource
    private Result result;
    @Resource
    private RedisUtil redisUtil;

    //登录
    @PostMapping("/login")
    public Result login(@RequestBody User users) {
        User user = usersService.findByUserPhone(users);
        if (user == null) throw new ServiceException("用户名或者密码错误");
        if (!users.getUserPassword().equals(user.getUserPassword())) throw new ServiceException("用户名或者密码错误");
        String token = TokenUtil.createToken(user.getUserId(), user.getUserPassword());
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        userVO.setToken(token);
        if (null == userVO) return result.fail("登录失败");
        return result.success("登录成功", userVO);
    }

    //    @AuthAccess
    @PostMapping("/register")
    public Result register(@RequestBody User users) {
        User user1 = usersService.findUser(users);
        if (user1 != null) return result.fail("注册失败该手机号已存在");
//        if (users.getUserPhone().equals(user.getUserPhone())) return result.fail("注册失败该手机号已存在");
        User user = new User();
        user.setUserName("宠物帮用户");
        user.setUserAvatar("http://dingyue.ws.126.net/2021/0909/3df9e6fcj00qz5vmz0015c000hs00hsc.jpg");
        user.setUserSex(1);
        user.setUserTime(LocalDateTime.now());
        user.setUserPhone(users.getUserPhone());
        user.setUserPassword(users.getUserPassword());
        user.setUserIntro("这个人很懒，什么都没写");
        Integer integer = usersService.addUser(user);
        if (integer > 0) return result.success("注册成功");
        return result.fail("注册失败");
    }

    @GetMapping("/{userId}")
    public Result findUser(@PathVariable Integer userId) {
        User user = usersService.findUserById(userId);
        return result.success(user);
    }


    @PostMapping("/updateinfo")
    public Result updateUser(@RequestBody User users) {
        User user1 = TokenUtil.getCurrenUser();
        User user = new User();
        user.setUserId(user1.getUserId());
        user.setUserName(users.getUserName());
        user.setUserSex(users.getUserSex());
        user.setUserAvatar(users.getUserAvatar());
        user.setUserIntro(users.getUserIntro());
        int i = usersService.updateUser(user);
        if (i < 0) return result.fail("修改失败");
        User user2 = usersService.findUserById(user1.getUserId());
        return result.success("修改成功", user2);
    }
}
